package com.example.springboot.Controller;

import cn.hutool.core.annotation.Alias;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.News;
import com.example.springboot.entity.Notice;
import com.example.springboot.entity.User;
import com.example.springboot.service.NewsService;
import com.example.springboot.service.UserService;
import com.example.springboot.utils.TokenUtils;
import lombok.Builder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * 功能:
 * 作者：jian
 * 日期：2024/3/14 15:29
 * @CrossOrigin 解决跨域
 */
@CrossOrigin
@RestController
@RequestMapping("/news")
@Builder
public class NewsController {

    @Autowired
    NewsService newsService;
    @Autowired
    UserService userService;





    @GetMapping("/selectNewsData")
    public Result selectNewsData(){
        List<News> list = newsService.list();
        return Result.success(list);
    }

    /**
     * 新增用户信息
     */
    @PostMapping("/add")
    public Result add(@RequestBody News news) {

        User currentUser = TokenUtils.getCurrentUser();
        news.setAuthorid(currentUser.getId());
        news.setTime(DateUtil.now()); //2024-03-25 15:15:20
        newsService.save(news);
        return Result.success(news);
    }


    /**
     * 查询所有用户信息
     *
     * @return
     */
    @GetMapping("/selectAll")
    public Result get() {
        List news = newsService.list(new QueryWrapper<News>().orderByDesc("id"));//select * from news order by id desc
        return Result.success(news);
    }


    /**
     * 根据用户id查询用户信息
     *
     * @param id
     * @return
     */
    //json数据传 {"id"="2"}
    @GetMapping("/selectById/{id}")
    public Result getUser(@PathVariable Integer id) {

        News news = newsService.getById(id);
        User user = userService.getById(news.getAuthorid());
        if (user!=null){
            news.setAuthor(user.getName());
        }
        return Result.success(news);
    }

    /**
     * 根据id更改用户信息
     *
     * @param news
     * @return
     */

    @PutMapping("/update")
    public Result update(@RequestBody News news) {
        System.out.println(news);
        newsService.updateById(news);

        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {

         newsService.removeById(id);
         return Result.success();
    }
//    http://localhost:8080/news/delete/46
    //json 数据传数组[1,2]
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        newsService.removeBatchByIds(ids);


        return Result.success();
    }


    /**
     * 模糊查询分页
     * @param pageNum  当前页码
     * @param pageSize 每页有多少个
     * @param title
     * @return
     */
    @GetMapping("/selectByPage")
    public Result getPage(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam String title) {
        QueryWrapper<News> queryWrapper =new  QueryWrapper<News>().orderByDesc("id");//默认倒叙
        queryWrapper.like(StrUtil.isNotBlank(title),"title",title);
        Page<News> page = newsService.page(new Page<>(pageNum, pageSize), queryWrapper);
        List<News> records = page.getRecords();

//        List<User> list = userService.list();
        for (News record : records) {
            Integer authorid = record.getAuthorid();
            User user = userService.getById(authorid);
            if (user!=null){
                record.setAuthor(user.getName());
            }
        }
        return Result.success(page);
    }




}